package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BanknoteSerialNumberRecord;
import com.ruoyi.system.domain.NiceRecord;
import com.ruoyi.system.service.IBanknoteSerialNumberRecordService;
import com.ruoyi.system.service.INiceRecordService;
import com.ruoyi.system.service.IUserMoneyService;
import com.ruoyi.system.utils.SerialNumberChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户操作冠字号记录Controller
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/user/moneyRecord")
public class UserCodeController extends BaseController {

    @Autowired
    private IUserMoneyService userMoneyService;

    @Autowired
    private INiceRecordService niceRecordService;

    @Autowired
    private IBanknoteSerialNumberRecordService banknoteSerialNumberRecordService;

    /**
     * 根据号码生成标十数据
     */
    @GetMapping("/listMy")
    public TableDataInfo listMy(BanknoteSerialNumberRecord record)
    {
        //生成完整的标十号码
        List<BanknoteSerialNumberRecord> codes = userMoneyService.listMy(record);
        return getDataTable(codes);
    }



    /**
     * 删除冠字号记录
     */
    @Log(title = "冠字号记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteMy/{ids}")
    public AjaxResult deleteMy(@PathVariable Long[] ids)
    {
        if (ids == null || ids.length == 0){
            return AjaxResult.success();
        }
        List<BanknoteSerialNumberRecord> list = banknoteSerialNumberRecordService.selectByIdsAndMy(ids);
        if (!list.isEmpty()){
            throw new ServiceException("抱歉，你输入的这些号码中，有非自己的号码:"+ list.stream().map(BanknoteSerialNumberRecord::getSerialNumber).collect(Collectors.joining(",")));
        }
        return toAjax(banknoteSerialNumberRecordService.deleteBanknoteSerialNumberRecordByIds(ids));
    }





    /**
     * 根据号码生成标十数据
     */
    @GetMapping("/createTenCodes/{code}")
    public TableDataInfo createTenCodes(@PathVariable String code)
    {
        //生成完整的标十号码
        String[] codes = SerialNumberChecker.checkAndGenerateSerialNumbers(code);
        return getDataTable(Arrays.asList(codes));
    }
    /**
     * 根据号码生成标百数据
     */
    @GetMapping("/createTensCodes/{code}")
    public TableDataInfo createTensCodes(@PathVariable String code)
    {
        //生成完整的标百号码
        String[] codes = SerialNumberChecker.checkAndGenerateSerialHundreds(code);
        return getDataTable(Arrays.asList(codes));
    }

    /**
     * 生成连续号
     */
    @GetMapping("/createContinuousCodes/{codeOne}/{codeTwo}")
    public TableDataInfo createContinuousCodes(@PathVariable String codeOne, @PathVariable String codeTwo)
    {
        //生成完整的标百号码
        List<String> codes = SerialNumberChecker.generateConsecutiveNumbers(codeOne, codeTwo);
        return getDataTable(codes);
    }

    /**
     * 新增冠字号记录
     */
    @Log(title = "用户冠字号记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertOne")
    public AjaxResult insertOne(@RequestBody BanknoteSerialNumberRecord banknoteSerialNumberRecord)
    {
        return toAjax(userMoneyService.insertOne(banknoteSerialNumberRecord));
    }

    /**
     * 用户标十冠字号记录
     */
    @Log(title = "用户标十冠字号记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertTen")
    public AjaxResult insertTen(@RequestBody List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords)
    {
        return toAjax(userMoneyService.insertTen(banknoteSerialNumberRecords));
    }

    /**
     * 用户标百冠字号记录
     */
    @Log(title = "用户标百冠字号记录", businessType = BusinessType.INSERT)
    @PostMapping("/insertTens")
    public AjaxResult insertTens(@RequestBody List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords)
    {
        return toAjax(userMoneyService.insertTens(banknoteSerialNumberRecords));
    }



    /**
     * 新增连续号
     */
    @PostMapping("/insertContinuousCodes")
    public AjaxResult insertContinuousCodes(@RequestBody List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords)
    {
        return toAjax(userMoneyService.insertContinuousCodes(banknoteSerialNumberRecords));
    }


    /**
     * 获取我自己的靓号
     */
    @GetMapping("/getMyNiceCodes")
    public TableDataInfo getMyNiceCodes( NiceRecord niceRecord)
    {
        niceRecord.setOwner(SecurityUtils.getUsername());
        List<NiceRecord> list = niceRecordService.selectNiceRecordList(niceRecord);
        return getDataTable( list);
    }

    /**
     * 获取配号记录
     */
    @GetMapping("/getMatching")
    public TableDataInfo getMatching()
    {
//        return null;
        return getDataTable(userMoneyService.getMatching());
    }



    /**
     * 新增连续号
     */
    @GetMapping("/getNoMyNiceCodes")
    public TableDataInfo getNoMyNiceCodes( NiceRecord niceRecord)
    {
        niceRecord.setStatus(1);
        List<NiceRecord> list = niceRecordService.selectNiceRecordList(niceRecord);

        return getDataTable(list);
    }

    /**
     * 修改靓号记录
     */
    @PutMapping("/updateMyNiceCord")
    public AjaxResult updateMyNiceCord(@RequestBody NiceRecord niceRecord)
    {
        return toAjax(userMoneyService.updateMyNiceCord(niceRecord));
    }

}
